package com.dongjiantong.farmrecords.model;
/**
 *  治污中心
 */

import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import lombok.Data;

import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Data
@Table(name = "farmrecords_pollution")
public class FarmRecordsPollution extends BaseEntity {
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -3657899912684075225L;

    /**
     * 单据编号
     */
    private String number;
    /**
     * 养殖场id
     */
    private Long farm_id;
    /**
     * 养殖场名称
     */
    private String farm_name;
    /**
     * 养殖品种
     */
    private Integer breed_second_type;
    /**
     * 省id
     */
    private Integer province_id;

    /**
     * 市id
     */
    private Integer city_id;

    /**
     * 县id
     */
    private Integer region_id;

    /**
     * 镇id
     */
    private Integer street_id;

    /**
     * 村id
     */
    private Integer village_id;
    /**
     * 记录日期
     */
    private Date date;
    /**
     * 干粪利用量（千克）
     */
    private Float dry_used_quantity;
    /**
     * 干粪利用去向
     */
    private String dry_used_to;
    /**
     * 干粪利用人
     */
    private String dry_used_user;
    /**
     * 沼液利用量（吨）
     */
    private Float liquid_used_quantity;
    /**
     * 沼液利用去向
     */
    private String liquid_used_to;
    /**
     * 沼液利用人
     */
    private String liquid_used_user;
    /**
     * 状态 0.暂存  2 提交
     */
    private Integer status;
    /**
     * 单据是否有效
     */
    private Integer delete_flag;
    /**
     * 创建日期
     */
    private Date created_at;
    /**
     * 更新日期
     */
    private Date updated_at;

    /**
     *  安全隐患照片数组
     */
    @JsonIgnore
    private String pitfall;
    @Transient
    private List<String> pitfallList;

    /**
     * 运行故障照片数组
     */
    @JsonIgnore
    private String abnormal;
    @Transient
    private List<String> abnormalList;

    /**
     * 有无安全隐患码，0无1有
     */
    private Integer is_pitfall;

    /**
     * 设施运行状态码  0: '正常',1: '沼气工程设施故障',2: '雨污分流设施故障',3: '干湿分离设施故障',4: '沼液利用设施故障',
     */
    private Integer run_status;

    /**
     * 关联的文件表图片list
     */
    @Transient
    private List picList;

    /**
     * 关联的文件表视频list
     */
    @Transient
    private List videoList;

    public void setPitfall(String pitfall) {
        this.pitfall = pitfall;

        if (Strings.isNullOrEmpty(pitfall)) {
            this.pitfallList = null;
        } else {
            try {
                this.pitfallList = objectMapper.readValue(pitfall, new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setPitfallList (List<String> pitfallList) {
        this.pitfallList = pitfallList;

        if (pitfallList == null && pitfallList.isEmpty()) {
            this.pitfall = null;
        } else {
            try {
                this.pitfall = objectMapper.writeValueAsString(pitfallList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setAbnormal (String abnormal) {
        this.abnormal = abnormal;

        if (Strings.isNullOrEmpty(abnormal)) {
            this.abnormalList = null;
        } else {
            try {
                this.abnormalList = objectMapper.readValue(abnormal, new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setAbnormalList (List<String> abnormalList) {
        this.abnormalList = abnormalList;

        if (abnormalList == null && abnormalList.isEmpty()) {
            this.abnormal = null;
        } else {
            try {
                this.abnormal = objectMapper.writeValueAsString(abnormalList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }
}